package com.phicomm.appmanager;

import java.security.Permissions;
import java.util.ArrayList;

import android.Manifest.permission;
import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.phicomm.appmanager.util.AppPermissionString;
import com.phicomm.mobilesecurity.R;
import com.phicomm.view.TitleBar;

public class AppPermissionActivity extends Activity {
	
	private TitleBar mTitleBar;
	public static final String EXTRA_APP_CHARGE_PERMISSION = "com.phicomm.appmanager.app_charge_permission";
	public static final String EXTRA_APP_PRIVATE_PERMISSION = "com.phicomm.appmanager.app_private_permission";
	public static final String EXTRA_APP_SETTING_PERMISSION = "com.phicomm.appmanager.app_setting_permission";
	public static final String EXTRA_APP_UID = "com.phicomm.appmanager.app_uid";
	public static final String EXTRA_APP_NAME = "com.phicomm.appmanager.app_name";
	private String mAppName;
	private ArrayList<String> mAppChargePermission;
	private ArrayList<String> mAppPrivatePermission;
	private ArrayList<String> mAppSettingPermission;
	
	private ListView mLsChargePermission;
	private ListView mLsPrivatePermission;
	private ListView mLsSettingPermission;
	
	private ArrayAdapter<String> mChargeAdapter;
	private ArrayAdapter<String> mPrivateAdapter;
	private ArrayAdapter<String> mSettingAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_app_permission);
		Intent intent = getIntent();
		Bundle data = intent.getExtras();
		mAppName = data.getString(EXTRA_APP_NAME);
		mAppChargePermission = data.getStringArrayList(EXTRA_APP_CHARGE_PERMISSION);
		mAppPrivatePermission = data.getStringArrayList(EXTRA_APP_PRIVATE_PERMISSION);
		mAppSettingPermission = data.getStringArrayList(EXTRA_APP_SETTING_PERMISSION);
		initView();
	}
	
	private void initView(){
		mTitleBar = (TitleBar) findViewById(R.id.tb_title);
		mTitleBar.setTitleText(0, mAppName);
		mTitleBar.setTitleTextSize(16);
		mTitleBar.setBackClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		
		mLsChargePermission = (ListView) findViewById(R.id.ls_charge);
		mLsPrivatePermission = (ListView) findViewById(R.id.ls_private);
		mLsSettingPermission = (ListView) findViewById(R.id.ls_setting);
		
		
		Log.d("tianfan", mAppChargePermission.size()+"charge");
		Log.d("tianfan",mAppPrivatePermission.size()+"private");
		Log.d("tianfan", mAppSettingPermission.size()+"settting");
		mChargeAdapter = new MyAdapter(mAppChargePermission);
		mPrivateAdapter = new MyAdapter(mAppPrivatePermission);
		mSettingAdapter = new MyAdapter(mAppSettingPermission);
		mLsChargePermission.setAdapter(mChargeAdapter);
		mLsPrivatePermission.setAdapter(mPrivateAdapter);
		mLsSettingPermission.setAdapter(mSettingAdapter);
		
	}
	
	private class MyAdapter extends ArrayAdapter<String>{
		public MyAdapter(ArrayList<String> permissions) {
			super(AppPermissionActivity.this, 0, permissions);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.app_permisson_list_item,	 null);
			    holder = new ViewHolder();
				holder.tvPermissionName = (TextView) convertView.findViewById(R.id.tv_permisson_name);
				holder.tvPermissionDescription = (TextView) convertView.findViewById(R.id.tv_permission_description);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			switch (getItem(position)) {
			case "android.permission.CALL_PHONE":
				setView(holder, R.string.perName_call, R.string.perDes_call);
				break;
			case "android.permission.SEND_SMS":
				setView(holder, R.string.perName_sms, R.string.perDes_sms);
				break;
			case "android.permission.MODIFY_PHONE_STATE":
				setView(holder, R.string.perName_traffic, R.string.perDes_traffic);
				break;
			case "android.permission.READ_SMS":
				setView(holder, R.string.perName_read_sms, R.string.perDes_read_sms);
				break;
			case "android.permission.READ_CALL_LOG":
				setView(holder, R.string.perName_read_call, R.string.perDes_read_call);
				break;
			case "android.permission.READ_CONTACTS":
				setView(holder, R.string.perName_read_contact, R.string.perDes_read_contact);
				break;
			case "android.permission.RECORD_AUDIO":
				setView(holder, R.string.perName_record, R.string.perDes_record);
				break;
			case "android.permission.ACCESS_COARSE_LOCATION":
				setView(holder, R.string.perName_locate, R.string.perDes_locate);
				break;
			case "android.permission.CAMERA":
				setView(holder, R.string.perName_camera, R.string.perDes_camera);
				break;
			case "android.permission.CHANGE_WIFI_STATE":
				setView(holder, R.string.perName_wifi, R.string.perDes_wifi);
				break;
			case "android.permission.WRITE_SETTINGS":
				setView(holder, R.string.perName_setting, R.string.perDes_setting);
				break;
			case "android.permission.BLUETOOTH_ADMIN":
				setView(holder, R.string.perName_bluetooth, R.string.perDes_bluetooth);
				break;
			case "android.permission.RECEIVE_BOOT_COMPLETED":
				setView(holder, R.string.PerName_boot, R.string.PerDes_boot);
				break;
			default:
				break;
			}
//			holder.tvPermissionName.setText(getItem(position));
//			holder.tvPermissionDescription.setText(getItem(position));
			return convertView;
		}
		
		public void setView(ViewHolder holder, int nameID, int desID){
			holder.tvPermissionName.setText(getString(nameID));
			holder.tvPermissionDescription.setText(getString(desID));
		}
		
		
	}
	
	private static class ViewHolder{
		public TextView tvPermissionName;
		public TextView tvPermissionDescription;
	}
	
}
